package gui.tab;

import gui.TuristAssistant;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.example.www.countryInfo.Array;
import org.example.www.countryInfo.Location;
import org.example.www.countryInfoRouteService.Tramo;

import com.cloudgarden.resource.SWTResourceManager;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class RouteMapCompositeTab extends org.eclipse.swt.widgets.Composite {

	{
		//Register as a resource user - SWTResourceManager will
		//handle the obtaining and disposing of resources
		SWTResourceManager.registerResourceUser(this);
	}
	
	private Text 						textResultsViewer;
	private Combo 						comboLocationFrom;
	private Label 						destinationsListlabel;
	private Label 						resultViewerlabel;
	private Label 						iconResultViewerLabel;
	private List 						listLocationsUnselected;
	private List 						listDestinations;		
	private Group 						groupCalculos;
	private Button 						buttonAddDestination;
	private Button 						buttonRadioTraceTourOption;
	private Button 						buttonRadioShortestPathOption;
	private Button 						buttonRadioDistanceOption;
	private Button 						buttonQuitDestination;
	private Button 						buttonCalcular;

	private Hashtable<String,Location> 	nameLocationsMap;	
	
	
	
	/**
	* Auto-generated main method to display this 
	* org.eclipse.swt.widgets.Composite inside a new Shell.
	*/
	public static void main(String[] args) {
		showGUI();
	}
		
	/**
	* Auto-generated method to display this 
	* org.eclipse.swt.widgets.Composite inside a new Shell.
	*/
	public static void showGUI() {
		Display display = Display.getDefault();
		Shell shell = new Shell(display);
		RouteMapCompositeTab inst = new RouteMapCompositeTab(shell, SWT.NULL);
		Point size = inst.getSize();
		shell.setLayout(new FillLayout());
		shell.layout();
		if(size.x == 0 && size.y == 0) {
			inst.pack();
			shell.pack();
		} else {
			Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
			shell.setSize(shellBounds.width, shellBounds.height);
		}
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	public RouteMapCompositeTab(org.eclipse.swt.widgets.Composite parent, int style) {
		super(parent, style);		
		nameLocationsMap = new Hashtable<String, Location>();
		ArrayList<Location> availableLocations = (TuristAssistant.infoPlacesClient.getLocations()).getElements();
		nameLocationsMap = new Hashtable<String,Location>();
		for (int i = 0; i < availableLocations.size(); i++)
			nameLocationsMap.put(availableLocations.get(i).getName(), availableLocations.get(i));
				
		initGUI();
	}

	private void initGUI() {
		try {
			this.setSize(397, 252);
			FormLayout thisLayout = new FormLayout();
			this.setLayout(thisLayout);
			this.setEnabled(true);
			this.addMouseTrackListener(new MouseTrackAdapter() {
				@Override
				public void mouseHover(MouseEvent evt) {
					thisMouseHover(evt);
				}
			});
			{
				listLocationsUnselected = new List(this, SWT.NONE | SWT.BORDER);
				FormData listLocationsUnselectedLData = new FormData();
				listLocationsUnselectedLData.width = 161;//-7;
				listLocationsUnselectedLData.height = 120;//-4;
				listLocationsUnselectedLData.left =  new FormAttachment(470, 1000, 0);
				listLocationsUnselectedLData.right =  new FormAttachment(100, 1000, 0);
				listLocationsUnselectedLData.top =  new FormAttachment(100, 1000, 0);
				listLocationsUnselectedLData.bottom =  new FormAttachment(1, 1000, 0);
				listLocationsUnselected.setLayoutData(listLocationsUnselectedLData);
				listLocationsUnselected.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
				listLocationsUnselected.addSelectionListener(new SelectionAdapter() {
					@Override
					public void widgetSelected(SelectionEvent evt) {
						listLocationsUnselectedWidgetSelected(evt);
					}
				});
				listLocationsUnselected.setVisible(false);
				for(Enumeration<String> e = nameLocationsMap.keys(); e.hasMoreElements();)
					listLocationsUnselected.add(e.nextElement());
				
			}
			{
				buttonAddDestination = new Button(this, SWT.TOGGLE | SWT.FLAT | SWT.CENTER);
				buttonAddDestination.setText("+ Destiny");
				FormData buttonAddDestinationLData = new FormData();
				buttonAddDestinationLData.width = 61;
				buttonAddDestinationLData.height = 24;
				buttonAddDestinationLData.left =  new FormAttachment(318, 1000, 0);
				buttonAddDestinationLData.right =  new FormAttachment(472, 1000, 0);
				buttonAddDestinationLData.top =  new FormAttachment(259, 1000, 0);
				buttonAddDestinationLData.bottom =  new FormAttachment(355, 1000, 0);
				buttonAddDestination.setLayoutData(buttonAddDestinationLData);
				buttonAddDestination.setEnabled(true);
				buttonAddDestination.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseUp(MouseEvent evt) {
						buttonAddDestinationMouseUp(evt);
					}
				});
								
			}
			{
				buttonQuitDestination = new Button(this, SWT.PUSH | SWT.CENTER);
				buttonQuitDestination.setText("- Destiny");
				FormData buttonQuitDestinationLData = new FormData();
				buttonQuitDestinationLData.width = 61;
				buttonQuitDestinationLData.height = 24;
				buttonQuitDestinationLData.left =  new FormAttachment(316, 1000, 0);
				buttonQuitDestinationLData.right =  new FormAttachment(469, 1000, 0);
				buttonQuitDestinationLData.top =  new FormAttachment(438, 1000, 0);
				buttonQuitDestinationLData.bottom =  new FormAttachment(533, 1000, 0);
				buttonQuitDestination.setLayoutData(buttonQuitDestinationLData);
				buttonQuitDestination.setEnabled(false);
				buttonQuitDestination.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseUp(MouseEvent evt) {
						buttonQuitDestinationMouseUp(evt);
					}					
				});
			}
			{
				textResultsViewer = new Text(this, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL | SWT.BORDER);
				textResultsViewer.setEditable(false);
				textResultsViewer.setBackground(SWTResourceManager.getColor(243, 255, 242));
				FormData textResultsViewerLData = new FormData();
				textResultsViewerLData.width = 161;
				textResultsViewerLData.height = 122;
				textResultsViewerLData.left =  new FormAttachment(512, 1000, 0);
				textResultsViewerLData.top =  new FormAttachment(446, 1000, 0);
				textResultsViewerLData.right =  new FormAttachment(991, 1000, 0);
				textResultsViewerLData.bottom =  new FormAttachment(954, 1000, 0);
				textResultsViewer.setLayoutData(textResultsViewerLData);
				textResultsViewer.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_SIZEALL));
			}
			{
				comboLocationFrom = new Combo(this, SWT.NONE);
				comboLocationFrom.setText("Choose leave point");
				comboLocationFrom.setOrientation(SWT.HORIZONTAL);
				FormData comboLocationFromLData = new FormData();
				comboLocationFromLData.width = 136;
				comboLocationFromLData.height = 23;
				comboLocationFromLData.left =  new FormAttachment(46, 1000, 0);
				comboLocationFromLData.right =  new FormAttachment(457, 1000, 0);
				comboLocationFromLData.top =  new FormAttachment(61, 1000, 0);
				comboLocationFromLData.bottom =  new FormAttachment(152, 1000, 0);
				comboLocationFrom.setLayoutData(comboLocationFromLData);
				comboLocationFrom.addSelectionListener(new SelectionAdapter() {
					@Override
					public void widgetSelected(SelectionEvent evt) {
						comboLocationFromWidgetSelected(evt);
					}
				});
				
				for(Enumeration<String> e = nameLocationsMap.keys(); e.hasMoreElements();)
					comboLocationFrom.add(e.nextElement());
					
								
			}

			{
				groupCalculos = new Group(this, SWT.NONE);
				groupCalculos.setLayout(null);
				groupCalculos.setText("Get");
				groupCalculos.setEnabled(true);
				groupCalculos.setData("RadioDistanceOption", buttonRadioDistanceOption);
				groupCalculos.setData("RadioShortestPathOption", buttonRadioShortestPathOption);
				FormData groupCalculosLData = new FormData();
				groupCalculosLData.width = 103;
				groupCalculosLData.height = 76;
				groupCalculosLData.left =  new FormAttachment(28, 1000, 0);
				groupCalculosLData.right =  new FormAttachment(303, 1000, 0);
				groupCalculosLData.top =  new FormAttachment(589, 1000, 0);
				groupCalculosLData.bottom =  new FormAttachment(962, 1000, 0);
				groupCalculos.setLayoutData(groupCalculosLData);
				groupCalculos.setData("RadioTraceTourOption", buttonRadioTraceTourOption);
				
				{
					buttonRadioDistanceOption = new Button(groupCalculos, SWT.RADIO | SWT.LEFT);
					buttonRadioDistanceOption.setText("Distance");
					buttonRadioDistanceOption.setBounds(13, 12, 125, 30);
					buttonRadioDistanceOption.setEnabled(false);
					buttonRadioDistanceOption.addMouseListener(new MouseAdapter() {
						@Override
						public void mouseUp(MouseEvent evt) {
							buttonRadioDistanceOptionMouseUp(evt);
						}
					});
				}							
				{
					buttonRadioShortestPathOption = new Button(groupCalculos, SWT.RADIO | SWT.LEFT);
					buttonRadioShortestPathOption.setText("Shortest Path");
					buttonRadioShortestPathOption.setBounds(13, 35, 125, 28);
					buttonRadioShortestPathOption.setEnabled(false);
					buttonRadioShortestPathOption.addMouseListener(new MouseAdapter() {
						@Override
						public void mouseUp(MouseEvent evt) {
							buttonRadioShortestPathOptionMouseUp(evt);
						}
					});
				}
				{
					buttonRadioTraceTourOption = new Button(groupCalculos, SWT.RADIO | SWT.LEFT);
					buttonRadioTraceTourOption.setText("Shortest Tour");
					buttonRadioTraceTourOption.setBounds(13, 61, 125, 28);
					buttonRadioTraceTourOption.setEnabled(false);
					buttonRadioTraceTourOption.addMouseListener(new MouseAdapter() {
						@Override
						public void mouseUp(MouseEvent evt) {
							buttonRadioTraceTourOptionMouseUp(evt);
						}
					});
				}
				
			}
			
			
			{
				listDestinations = new List(this, SWT.NONE | SWT.BORDER);
				FormData listDestinationsLData = new FormData();
				listDestinationsLData.width = 99;
				listDestinationsLData.height = 68;
				listDestinationsLData.left =  new FormAttachment(31, 1000, 0);
				listDestinationsLData.right =  new FormAttachment(298, 1000, 0);
				listDestinationsLData.top =  new FormAttachment(255, 1000, 0);
				listDestinationsLData.bottom =  new FormAttachment(541, 1000, 0);
				listDestinations.setLayoutData(listDestinationsLData);
			}
			{
				buttonCalcular = new Button(this, SWT.PUSH | SWT.CENTER);
				buttonCalcular.setText("OK");
				FormData buttonCalcularLData = new FormData();
				buttonCalcularLData.width = 41;
				buttonCalcularLData.height = 30;
				buttonCalcularLData.left =  new FormAttachment(358, 1000, 0);
				buttonCalcularLData.right =  new FormAttachment(462, 1000, 0);
				buttonCalcularLData.top =  new FormAttachment(720, 1000, 0);
				buttonCalcularLData.bottom =  new FormAttachment(839, 1000, 0);
				buttonCalcular.setLayoutData(buttonCalcularLData);
				buttonCalcular.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseUp(MouseEvent evt) {
						buttonCalcularMouseUp(evt);
					}
				});
				buttonCalcular.setEnabled(false);
			}
			{
				resultViewerlabel = new Label(this, SWT.NONE);
				resultViewerlabel.setText("Results Viewer");				
				FormData resultViewerlabelLData = new FormData();
				resultViewerlabelLData.width = 88;
				resultViewerlabelLData.height = 16;
				resultViewerlabelLData.left =  new FormAttachment(517, 1000, 0);
				resultViewerlabelLData.right =  new FormAttachment(739, 1000, 0);
				resultViewerlabelLData.top =  new FormAttachment(367, 1000, 0);
				resultViewerlabelLData.bottom =  new FormAttachment(430, 1000, 0);
				resultViewerlabel.setLayoutData(resultViewerlabelLData);
			}
			{
				destinationsListlabel = new Label(this, SWT.NONE);
				destinationsListlabel.setText("Destinations list");
				FormData destinationsListlabelLData = new FormData();
				destinationsListlabelLData.width = 81;
				destinationsListlabelLData.height = 18;
				destinationsListlabelLData.left =  new FormAttachment(41, 1000, 0);
				destinationsListlabelLData.right =  new FormAttachment(245, 1000, 0);
				destinationsListlabelLData.top =  new FormAttachment(180, 1000, 0);
				destinationsListlabelLData.bottom =  new FormAttachment(251, 1000, 0);
				destinationsListlabel.setLayoutData(destinationsListlabelLData);
			}
			{
				iconResultViewerLabel = new Label(this, SWT.NONE);
				iconResultViewerLabel.setImage(new Image(Display.getCurrent(),
				"resource/mapaPatagonia.jpg"));
				FormData iconResultViewerLabelLData = new FormData();
				iconResultViewerLabelLData.width = 80;
				iconResultViewerLabelLData.height = 93;
				iconResultViewerLabelLData.left =  new FormAttachment(769, 1000, 0);
				iconResultViewerLabelLData.right =  new FormAttachment(971, 1000, 0);
				iconResultViewerLabelLData.top =  new FormAttachment(37, 1000, 0);
				iconResultViewerLabelLData.bottom =  new FormAttachment(406, 1000, 0);
				iconResultViewerLabel.setLayoutData(iconResultViewerLabelLData);
			}
			
			this.layout();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
		
		
	private void buttonAddDestinationMouseUp(MouseEvent evt) {
		System.out.println("buttonAddDestination.mouseUp, event="+evt);
		listLocationsUnselected.pack();
		listLocationsUnselected.setVisible(true);
	}
	
	
	private void buttonQuitDestinationMouseUp(MouseEvent evt) {
		int[] locationsSelected = listDestinations.getSelectionIndices();
		if (locationsSelected.length != 0){
			for (int i = 0; i < locationsSelected.length; i++){
				listLocationsUnselected.add(nameLocationsMap.get(listDestinations.getItem(locationsSelected[i])).getName());
				listDestinations.remove(listDestinations.getItem(locationsSelected[i]));
			}
			int cantLocations = listDestinations.getItemCount();
			if (cantLocations < 2 && cantLocations == 1){
				buttonRadioTraceTourOption.setEnabled(false);
				buttonRadioDistanceOption.setEnabled(true);
				buttonRadioShortestPathOption.setEnabled(true);
			}
				
			if (listDestinations.getItemCount() < 1 || comboLocationFrom.getSelectionIndex() == -1){
				buttonQuitDestination.setEnabled(false);
				buttonCalcular.setEnabled(false);			
				buttonRadioDistanceOption.setEnabled(false);
				buttonRadioShortestPathOption.setEnabled(false);		
			}			
		}		
	}
	
	private void listLocationsUnselectedWidgetSelected(SelectionEvent evt) {
		System.out.println("listLocationsUnselected.widgetSelected, event="+evt);
		
		String[] locationsSelected = listLocationsUnselected.getSelection();
		if (locationsSelected.length != 0){
			for (int i = 0; i < locationsSelected.length; i++){
				listDestinations.add(locationsSelected[i]);
				listLocationsUnselected.remove(locationsSelected[i]);
			}
			if (listDestinations.getItemCount() > 1 && comboLocationFrom.getSelectionIndex() != -1){
				buttonRadioTraceTourOption.setEnabled(true);
				buttonRadioDistanceOption.setEnabled(false);
				buttonRadioShortestPathOption.setEnabled(false);
			}
			if (listDestinations.getItemCount() == 1 && comboLocationFrom.getSelectionIndex() != -1){
				buttonRadioDistanceOption.setEnabled(true);
				buttonRadioShortestPathOption.setEnabled(true);		
			}			
			buttonCalcular.setEnabled(true);
			buttonQuitDestination.setEnabled(true);			
		}
	}
	
	private void buttonCalcularMouseUp(MouseEvent evt) {
		System.out.println("buttonCalcular.mouseUp, event="+evt);
		
		if (validLocations()){
			Location origin = nameLocationsMap.get(comboLocationFrom.getItem(comboLocationFrom.getSelectionIndex()));
			Location dest = nameLocationsMap.get(listDestinations.getItems()[0]);
			if (buttonRadioDistanceOption.getSelection()){			
				Integer result = new Integer(TuristAssistant.infoRouteClient.distanceBetween(origin, dest));
				textResultsViewer.setText("Distance between the locations is: "+result.toString()+" kms");				
			}
			if (buttonRadioShortestPathOption.getSelection()){
				ArrayList<Tramo> tramos = (ArrayList<Tramo>) TuristAssistant.infoRouteClient.shortestPathBetween(origin, dest);
				printTramos(tramos);			
			}
			if (buttonRadioTraceTourOption.getSelection()){
				Array<Location> destinations = new Array<Location>();
				String[] destinationItems = listDestinations.getItems();
				for (int i = 0; i < destinationItems.length; i++)
					destinations.addElement(nameLocationsMap.get(destinationItems[i]));
				ArrayList<Tramo> tramos = (ArrayList<Tramo>) TuristAssistant.infoRouteClient.traceJourney(origin,destinations);
				printTramos(tramos);				
			}		
		}
	}
	
	private boolean validLocations(){
		boolean validLocations = true;
		
		Location comboLocation = nameLocationsMap.get(comboLocationFrom.getItem(comboLocationFrom.getSelectionIndex()));
		String[] destinationsSelected = listDestinations.getItems();
		  
		for(int i = 0; validLocations && i < destinationsSelected.length; i++){
			Location destLocation = nameLocationsMap.get(destinationsSelected[i]);
			if(comboLocation.equals(destLocation)){
				validLocations = false;
				MessageBox dialog = new MessageBox(this.getShell(),this.getStyle()|SWT.ERROR|SWT.OK);
				dialog.setMessage("The leave location cannot be present at the destinations list");
				dialog.setText("Input Error");
				dialog.open();
			}
		}
		return validLocations;
	}
	
	private void printTramos(ArrayList<Tramo> tramos){
		textResultsViewer.setText("");
		for (int i = 0; i < tramos.size(); i++){
			textResultsViewer.append(tramos.get(i).toString());
			textResultsViewer.append("\n\n");
		}
	}
	
	private void thisMouseHover(MouseEvent evt) {
		System.out.println("this.mouseHover, event="+evt);
		listLocationsUnselected.setVisible(false);
	}
	
	private void buttonRadioDistanceOptionMouseUp(MouseEvent evt) {
		System.out.println("buttonRadioDistanceOption.mouseUp, event="+evt);
		buttonRadioTraceTourOption.setSelection(false);
		buttonRadioShortestPathOption.setSelection(false);
		buttonRadioDistanceOption.setSelection(true);		
	}
	
	private void buttonRadioShortestPathOptionMouseUp(MouseEvent evt) {
		System.out.println("buttonRadioShortestPathOption.mouseUp, event="+evt);
		buttonRadioTraceTourOption.setSelection(false);
		buttonRadioShortestPathOption.setSelection(true);
		buttonRadioDistanceOption.setSelection(false);
	}
	
	private void buttonRadioTraceTourOptionMouseUp(MouseEvent evt) {
		System.out.println("buttonRadioTraceTourOption.mouseUp, event="+evt);
		buttonRadioTraceTourOption.setSelection(true);
		buttonRadioShortestPathOption.setSelection(false);
		buttonRadioDistanceOption.setSelection(false);
	}
	
	private void comboLocationFromWidgetSelected(SelectionEvent evt) {
		System.out.println("comboLocationFrom.widgetSelected, event="+evt);
		int cantDestinations = listDestinations.getItemCount();
		if (comboLocationFrom.getSelectionIndex() != -1 && cantDestinations > 0){
			buttonCalcular.setEnabled(true);
			if (cantDestinations == 1){
				buttonRadioDistanceOption.setEnabled(true);
				buttonRadioShortestPathOption.setEnabled(true);					
			}
			else
				buttonRadioTraceTourOption.setEnabled(true);
		}
	}

}
